import java.io.Serializable;
import java.rmi.server.UID;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class Trajet implements Serializable{

    private static int id = 0;

    private Ville start;
    private Ville arrived;
    private Date startDate;
    private Date arrivedDate;
    private int freeCapacity;
    private List<Voyageur> reservations;
    private int idTrajet;

    public Trajet() {}

    public Trajet(Ville start, Ville arrived, Date startDate, Date arrivedDate) {
        this.start = start;
        this.arrived = arrived;
        this.startDate = startDate;
        this.arrivedDate = arrivedDate;
        this.freeCapacity = Transport.INIT_CAPACITY;
        this.reservations = new ArrayList<Voyageur>();
        Trajet.id++;
        this.idTrajet = Trajet.id;

    }

    public Trajet(Ville start, Ville arrived, Date startDate, Date arrivedDate, int idTrajet) {
        this(start, arrived, startDate, arrivedDate);   
        this.idTrajet = idTrajet;
    }

    public String toString() {
        String s =  start + "(" + this.getFormatedStartDate() + ")" + " ---- " + arrived + "(" + this.getFormatedArrivedDate() + ")\n";
        s+="\t\t\t+ Durée : " + getDuration() + "\n";
        s+="\t\t\t+ Il reste " + getFreeCapacity() + " place(s) de diponible(s) sur ce trajet\n";
        if (hasReservations()) {
            s += "\t\t\t+ Réservation(s) :\n";
            for (Voyageur v : getReservations()) {
                s += "\t\t\t\t" + v.toString() + "\n";
            }
        }
        return s;
    }


    public Date getStartDate() {
        return this.startDate;
    }

    public Date getArrivedDate() {
        return this.arrivedDate;
    }

    public String getFormatedStartDate() {

        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy 'à' HH:mm");
        return dateFormat.format(this.startDate);
    }

    public String getFormatedArrivedDate() {

        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy 'à' HH:mm");
        return dateFormat.format(this.arrivedDate);
    }

    public String getDuration() {
        Long heure = (this.getArrivedDate().getTime() - this.getStartDate().getTime())/3600000;
        Long minute = (this.getArrivedDate().getTime() - this.getStartDate().getTime())%3600000/60000;

        return heure +"h" + String.format("%02d", minute);
    }

    public boolean addReservation(Voyageur v) {
        if(freeCapacity > 0) {
            getReservations().add(v);
            freeCapacity--;
            return true;
        }
        else {
            return false;
        }        
    }

    /**
     * @return the start
     */
    public Ville getStart() {
        return start;
    }

    /**
     * @param start the start to set
     */
    public void setStart(Ville start) {
        this.start = start;
    }

    /**
     * @return the arrived
     */
    public Ville getArrived() {
        return arrived;
    }

    /**
     * @param arrived the arrived to set
     */
    public void setArrived(Ville arrived) {
        this.arrived = arrived;
    }

    /**
     * @param startDate the startDate to set
     */
    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    /**
     * @param arrivedDate the arrivedDate to set
     */
    public void setArrivedDate(Date arrivedDate) {
        this.arrivedDate = arrivedDate;
    }

    /**
     * @return the freeCapacity
     */
    public int getFreeCapacity() {
        return freeCapacity;
    }

    /**
     * @param freeCapacity the freeCapacity to set
     */
    public void setFreeCapacity(int freeCapacity) {
        this.freeCapacity = freeCapacity;
    }

    /**
     * @return if the reservation is not empty
     */
    public boolean hasReservations() {
        return !(reservations.isEmpty());
    }

    /**
     * @return the reservations
     */
    public List<Voyageur> getReservations() {
        return reservations;
    }

    /**
     * @return the id of Trajet
     */
    public int getIdTrajet() {
        return idTrajet;
    }

    public boolean equals(Object o) {
        if(!this.arrived.equals(((Trajet)o).arrived)) {
            return false;
        } 
        else if(!this.arrivedDate.equals(((Trajet)o).arrivedDate)) {
            return false;
        }
        else if(this.freeCapacity != ((Trajet)o).freeCapacity) {
            return false;
        }
        else if(this.idTrajet != ((Trajet)o).idTrajet) {
            return false;
        }
        else if(!this.start.equals(((Trajet)o).start)) {
            return false;
        }
        else if(!this.startDate.equals(((Trajet)o).startDate)) {
            return false;
        }
        else if(!this.reservations.equals(((Trajet)o).reservations)) {
            return false;
        }
        else {
            return true;
        }

    }


}
